package com.future.hedongjun.home.bean.response;

import com.future.baselib.entity.BaseResponse;
import com.future.hedongjun.home.bean.BannerBean;
import com.future.hedongjun.home.bean.CategoryBean;
import com.future.hedongjun.home.bean.ThemeBean;
import com.future.hedongjun.home.bean.TopicBean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class HomeResponse extends BaseResponse {

    public List<BannerBean> bannerBeans;
    public List<CategoryBean> categoryBeans;
    public List<ThemeBean> themeBeans;
    public List<TopicBean> topicBeans;

    @Override
    public void parseInfo(String content) throws JSONException {
        JSONObject object = new JSONObject(content);
        Gson gson = new Gson();
        bannerBeans = gson.fromJson(object.optString("banner"),new TypeToken<List<BannerBean>>(){}.getType());
        categoryBeans = gson.fromJson(object.optString("cate"),new TypeToken<List<CategoryBean>>(){}.getType());
        topicBeans = gson.fromJson(object.optString("course"),new TypeToken<List<TopicBean>>(){}.getType());

        themeBeans = new ArrayList<>();
        JSONArray themeArray = object.optJSONArray("theme");
        for (int i = 0; i < themeArray.length(); i++) {
            JSONObject themeObject = themeArray.optJSONObject(i);
            themeBeans.add(new ThemeBean(true,themeObject.optString("theme_name"),themeObject.optString("theme_id")));
            JSONArray course_list = themeObject.optJSONArray("course_list");
            for (int j = 0; j < course_list.length(); j++) {
                themeBeans.add(new ThemeBean(new Gson().fromJson(course_list.optString(j),TopicBean.class)));
            }
        }

    }
}
